/**
 * jims
 */
package com.jims.master.modules.blood.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.NumberUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.blood.dao.BloodApplyDao;
import com.jims.master.modules.blood.dao.BloodInpDetailDao;
import com.jims.master.modules.blood.dao.BloodMatchDao;
import com.jims.master.modules.blood.dao.BloodOutSheetDao;
import com.jims.master.modules.blood.entity.BloodApply;
import com.jims.master.modules.blood.entity.BloodMatch;
import com.jims.master.modules.blood.entity.BloodOutSheet;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 发血单Service
 * @author CTQ
 * @version 2017-04-13
 */
@Service
@Transactional(readOnly = true)
public class BloodOutSheetService extends CrudService<BloodOutSheetDao, BloodOutSheet> {
	@Autowired
	private BloodMatchDao bloodMatchDao;
	@Autowired
	private BloodInpDetailDao bloodInpDetailDao;
	@Autowired
	private BloodApplyDao bloodApplyDao;

	/**
	 * 根据参数查询需要发血数据
	 * @param bloodOutSheet
	 * @return
	 */
	public BaseDto findWaitOutSheet(BloodOutSheet bloodOutSheet){
		return dao.findWaitOutSheet(bloodOutSheet);
	}


	public List<BloodOutSheet> findList(BloodOutSheet bloodOutSheet) {
		return super.findList(bloodOutSheet);
	}
	
	public Page<BloodOutSheet> findPage(Page<BloodOutSheet> page, BloodOutSheet bloodOutSheet) {
		return super.findPage(page, bloodOutSheet);
	}
	
	@Transactional(readOnly = false)
	public int saveSheet(BloodOutSheet bloodOutSheet) {
		int num = 0;
		if (bloodOutSheet!=null){
			if (bloodOutSheet.getSheetList()!=null&&bloodOutSheet.getSheetList().size()>0){
				for (BloodOutSheet bos:bloodOutSheet.getSheetList()) {
					if (StringUtils.isNotBlank(bos.getApplyId())){
						/**1.更新配血表是否发血状态为已发血send_flag='1'**/
						BloodMatch bloodMatch = bloodMatchDao.get(bos.getMatchSheetId());
						bloodMatch.setSendFlag("1");
						bloodMatch.preUpdate();
						bloodMatchDao.update(bloodMatch);
						/**2.更新血液入库明细中库存状态为已出库flag='1'**/
						bloodInpDetailDao.updateFlagByBloodId("1",bos.getBloodId());
						/**3.判断是否有未发血的配血记录，如果没有，则更新用血申请单状态为已输血**/
						List<BaseDto> list = bloodMatchDao.findByApplyNum(bos.getApplyNum(),UserUtils.getOrgId());
						if(list.size()>0){
						}else{
							BloodApply bloodApply = bloodApplyDao.get(bos.getApplyId());
							bloodApply.setApplyStatus("2");
							bloodApply.preUpdate();
							bloodApplyDao.update(bloodApply);
						}
						/**4.保存发血信息**/
						bos.setBloodOutNum(NumberUtils.getOutSheet());
						bos.setLxOpertor(bloodOutSheet.getLxOpertor());
						bos.setStartDate(new Date());
						num = super.save(bos);
					}
				}
			}
		}
		return num;
	}

    /**
     * 发血单查询
     * @param bloodOutSheet
     * @return
     * @author fengyg
     */
    public List<BloodOutSheet> getBloodOutSheetList(BloodOutSheet bloodOutSheet){
        return dao.getBloodOutSheetList(bloodOutSheet);
    }

	@Transactional(readOnly = false)
	public int delete(BloodOutSheet bloodOutSheet) {
		return super.delete(bloodOutSheet);
	}
	
}